/**
 * Created by Adolph on 2017/3/31 0031.
 */

var username;

function displayAnswer(question, answer) {
    var answerNum;
    if (question.length === 2) {
        answerNum = "a" + question.charAt(1);
    } else if (question.length === 3) {
        answerNum = "a" + question.charAt(1) + question.charAt(2);
    }
    document.getElementById(answerNum).value = answer;
    document.getElementById(answerNum).style.backgroundColor = "#42a3f3";
    document.getElementById(answerNum).style.textAlign = "center";
}

function saveUser() {
    username = document.getElementById("user").value;
    if (username.length<1) {
        alert("未输入姓名");
    } else {
        document.getElementById('username').innerHTML="欢迎 "+username;
        $('#signInModal').modal('hide');
    }
}

function score() {
    var completeState = 1;
    var answerNum;
    for (var i = 1; i <= 40; i++) {
        answerNum = "a" + i;
        var state = document.getElementById(answerNum).value;
        if (state !== "A" && state !== "B" && state !== "C" && state !== "D") {
            document.getElementById(answerNum).style.backgroundColor = "#ff6985";
            completeState = 0;
        }
    }
    if (completeState == 0) {
        alert("还有未选择的题目！");
        scrollTo(0, 0);
    } else {
        sendData();
        $('#scoreModal').modal('toggle');
    }
    scrollTo(0, 0);
}

window.onload = function () {
    $('#signInModal').modal({backdrop:'static',keyboard:false});
    var request = new XMLHttpRequest();
    var fd = new FormData();
    request.open("post", "server.php", true);
    request.send(fd);

    request.onreadystatechange = function () {
        if (request.readyState === 4) {

            if (request.status === 200) {
                var resopne = request.responseText;
                var reArray = eval("(" + resopne + ")");
                document.getElementById("completeNum").innerHTML = "已完成人数 : " + reArray.max_number;
            } else {
                alert("发生错误：" + request.status);
            }
        }
    }
}

function sendData() {
    var fd = new FormData();
    var request = new XMLHttpRequest();
    var val = "{";
    for (var i = 1; i <= 40; i++) {
        var question = "\"q" + i + "\":\"";
        var answer = "a" + i;
        val += question;
        val += document.getElementById(answer).value + "\",";
    }
    val = val.substring(0, val.length - 1) + "}";

    fd.append("json", val);
    request.open("post", "./server.php", true);
    request.send(fd);

    request.onreadystatechange = function () {
        if (request.readyState === 4) {
            if (request.status === 200) {
                var resopne = request.responseText;
                var reArray = eval("(" + resopne + ")");
                var errorQ = reArray.error;
                if (errorQ[2] != undefined) {
                    var pos = 3;
                    while (true) {
                        var err;
                        if (errorQ[pos + 1] != "\"") {
                            err = "a" + errorQ[pos] + errorQ[pos + 1];
                            pos += 10;
                        } else {
                            err = "a" + errorQ[pos];
                            pos += 9;
                        }
                        document.getElementById(err).style.backgroundColor = "#ff9092";
                        if (errorQ[pos] == undefined) {
                            break;
                        }
                    }
                }
                document.getElementById("scoreList1").innerHTML = "姓名 ：" + username;
                if (parseInt(reArray.mark) >= 60) {
                    document.getElementById("scoreList2").style.color="#6bd97e";
                    document.getElementById("scoreList2").innerHTML = "分数 ：合格(" + reArray.mark + ")";
                }else{
                    document.getElementById("scoreList2").style.color="#d96d62";
                    document.getElementById("scoreList2").innerHTML = "分数 ：不合格(" + reArray.mark + ")";
                }
                document.getElementById("scoreList3").innerHTML = "高于 ：" + reArray.percentage;

                document.getElementById("mark").innerHTML = "成绩 : " + reArray.mark;
                document.getElementById("completeNum").innerHTML = "已完成人数 : " + reArray.max_number;
                document.getElementById("rank").innerHTML = "高于 : " + reArray.percentage;

            } else {
                alert("发生错误：" + request.status);
            }
        }
    }
}